/*
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.android.gcm.demo.server;

import static com.google.appengine.api.taskqueue.TaskOptions.Builder.withUrl;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;

/**
 * Servlet that adds a new message to all registered devices.
 * <p>
 * This servlet is used just by the browser (i.e., not device).
 */
@SuppressWarnings("serial")
public class SendTestMessage extends BaseServlet {

	/**
	 * Processes the request to add a new message.
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {

		resp.setContentType("text/html; charset=UTF-8");
		PrintWriter out = resp.getWriter();
		String username=req.getParameter("username");
		String password=req.getParameter("password");
		String regid=req.getParameter("regid");
		String titoloArticolo=req.getParameter("titoloArticolo");
		
		String status;
		
//		String username=Autenticazione.USERNAME;
//		String password=Autenticazione.PASSWORD;
//		String regid="APA91bF-_9Z3rFbvhB0YMyfuS0JaCGqT3TACCWjzEdQ15pZeofbYPW_GVc8lo7mDuct-HGSeM2aCd8Js99uy3W1MGHM0_6fHV4VbNpsRMjOVSVQgQKKhjB3eVFdb1NE5dwN6A5OpbakHdzi3QSzA9tjH0OST3E7V0DN-bbdVYge5Zvo3B9XF3qk";
//		String titoloArticolo="Titolo di prova";
		
		if(username.equals(Autenticazione.USERNAME) && password.equals(Autenticazione.PASSWORD))
		{
			Queue queue = QueueFactory.getQueue("gcm");
			queue.add(withUrl("/send").param(SendMessageServlet.PARAMETER_DEVICE, regid).param("titoloArticolo", titoloArticolo));
			status = "Single message queued for registration id " + regid;
			req.setAttribute(HomeServlet.ATTRIBUTE_STATUS, status.toString());
			out.println(status);
		}
		else
			out.println("Errore autenticazione!");

		out.close();
	}

}
